﻿using log4net;
using Nova_BLL;
using Nova_Common.Extend;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WMS_Api.App_Start;
using WMS_Api.Areas.WCSSystem.Models;
using WMS_Api.Controllers;
using WMS_Api.Models;

namespace WMS_Api.Areas.WCSSystem.Controllers
{
    /// <summary>
    /// WCS同步LCD任务信息功能
    /// </summary>
    [RoutePrefix("api/WCSSystem/LcdInfo")]
    [ControllerGroup("WCS", "同步LCD信息功能接口")]
    public class LcdInfoController : BaseApiController
    {
        TLCDTaskBll LcdTaskBll = new TLCDTaskBll();
        TCommandBll CmdBll = new TCommandBll();
        TLCDLogBll LcdLogBll = new TLCDLogBll();
        ILog _logger = LogManager.GetLogger(typeof(LcdInfoController));

        /// <summary>
        /// 同步LCD显示的任务信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost, Route("SyncTaskInfo")]
        public ApiResult SyncTaskInfo(LcdTaskInfoModel param)
        {
            _logger.InfoFormat("WCS同步LCD任务，请求参数:{0}", param.ToJson());
            if (param.WmsTaskId.IsEmpty())
                return Error(ResultCode.Error, "WmsTaskId不能为空!");
            if (param.DeviceCode.IsEmpty())
                return Error(ResultCode.Error, "DeviceCode不能为空!");
            LCDTaskInfoModel taskInfoModel = new LCDTaskInfoModel();
            var taskModel = CmdBll.GetEntityByTaskId(long.Parse(param.WmsTaskId));
            if (taskModel.TaskMode == 5) // 补货
                taskInfoModel = CmdBll.GetLcdReplenTaskInfoByTaskId(long.Parse(param.WmsTaskId));
            else
                taskInfoModel = CmdBll.GetLcdTaskInfoByTaskId(long.Parse(param.WmsTaskId));
            if (taskInfoModel.IsEmpty())
                return Error("此任务不存在任务表中!");
            var lcdModel = LcdTaskBll.GetBySQID(param.DeviceCode);
            long Pos_ID = taskInfoModel.PLCType == 1 ? taskInfoModel.PosE_ID : taskInfoModel.PosS_ID;
            bool res = false;
            if (lcdModel.IsEmpty())
            {
                TLCDTask entity = new TLCDTask()
                {
                    TaskID = taskInfoModel.TaskID,
                    SQID = int.Parse(param.DeviceCode),
                    Pos_ID = Pos_ID,
                    StockNo = taskInfoModel.StockNo,
                    Mat_Code = taskInfoModel.Mat_Code,
                    Mat_Name = taskInfoModel.Mat_Name,
                    SerialNum = taskInfoModel.TS01,
                    Mat_Num = taskInfoModel.MAT_NUM,
                    Tray_ID = taskInfoModel.Tray_ID,
                    S01 = string.Format("{0}|{1}", taskInfoModel.S01,taskInfoModel.TM01) ,
                    S05 = taskInfoModel.S05,
                    TaskMode = taskInfoModel.WMSTypeName,
                    UpdateTime = DateTime.Now
                };
                res = LcdTaskBll.InsertEntity(entity);
            }
            else
            {
                lcdModel.TaskID = taskInfoModel.TaskID;
                lcdModel.Pos_ID = Pos_ID;
                lcdModel.StockNo = taskInfoModel.StockNo;
                lcdModel.Mat_Code = taskInfoModel.Mat_Code;
                lcdModel.TaskMode = taskInfoModel.WMSTypeName;
                lcdModel.Mat_Name = taskInfoModel.Mat_Name;
                lcdModel.Mat_Num = taskInfoModel.MAT_NUM;
                lcdModel.SerialNum = taskInfoModel.TS01;
                lcdModel.Tray_ID = taskInfoModel.Tray_ID;
                lcdModel.S01 = string.Format("{0}|{1}", taskInfoModel.S01, taskInfoModel.TM01);
                lcdModel.S05 = taskInfoModel.S05;
                lcdModel.UpdateTime = DateTime.Now;
                res = LcdTaskBll.UpdateEntity(lcdModel);
            }
            return res ? Success("同步LCD显示的任务成功") : Error("同步LCD显示的任务失败");
        }

        /// <summary>
        /// 同步LCD显示的设备错误信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost, Route("SyncDeviceInfo")]
        public ApiResult SyncDeviceInfo(LcdDeviceInfoModel param)
        {
            if (param.DeviceCode.IsEmpty())
                return Error(ResultCode.Error, "DeviceCode不能为空!");
            var model = LcdLogBll.GetBySQID(param.DeviceCode);
            bool res = false;
            if (model.IsEmpty())
            {
                TLCDLog entity = new TLCDLog()
                {
                    SQID = int.Parse(param.DeviceCode),
                    Infor = param.ErrorMessage,
                    UpdateTime = param.Time
                };
                res = LcdLogBll.InsertEntity(entity);
            }
            else
            {
                model.Infor = param.ErrorMessage;
                model.UpdateTime = param.Time;
                res = LcdLogBll.UpdateEntity(model);
            }
            return res ? Success("同步LCD显示的设备错误信息成功!") : Error("同步LCD显示的设备错误信息失败");
        }


        /// <summary>
        /// 同步LCD显示的设备错误信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost, Route("SyncDeviceInfoList")]
        public ApiResult SyncDeviceInfoList(List<LcdDeviceInfoModel> param)
        {
            foreach (var device in param)
            {
                if (device.DeviceCode.IsEmpty())
                    continue;
                var model = LcdLogBll.GetBySQID(device.DeviceCode);
                if (model.IsEmpty())
                {
                    TLCDLog entity = new TLCDLog()
                    {
                        SQID = int.Parse(device.DeviceCode),
                        Infor = device.ErrorMessage,
                        UpdateTime = device.Time
                    };
                    LcdLogBll.InsertEntity(entity);
                }
                else
                {
                    model.Infor = device.ErrorMessage;
                    model.UpdateTime = device.Time;
                    LcdLogBll.UpdateEntity(model);
                }
            }
            return Success("同步LCD设备信息成功!");
        }
    }
}
